<?php

/**
 * factura actions.
 *
 * @package    courier
 * @subpackage factura
 * @author     Your name here
 */
class facturaActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->Facturas = FacturaPeer::doSelect(new Criteria());
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->Factura = FacturaPeer::retrieveByPk($request->getParameter('fac_id'));
    $this->forward404Unless($this->Factura);
  }

  public function executeShowFactura(sfWebRequest $request)
  {
    $this->Factura = FacturaPeer::retrieveByPk($request->getParameter('fac_id'));
    $this->forward404Unless($this->Factura);
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new FacturaForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new FacturaForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($Factura = FacturaPeer::retrieveByPk($request->getParameter('fac_id')), sprintf('Object Factura does not exist (%s).', $request->getParameter('fac_id')));
    $this->form = new FacturaForm($Factura);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($Factura = FacturaPeer::retrieveByPk($request->getParameter('fac_id')), sprintf('Object Factura does not exist (%s).', $request->getParameter('fac_id')));
    $this->form = new FacturaForm($Factura);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($Factura = FacturaPeer::retrieveByPk($request->getParameter('fac_id')), sprintf('Object Factura does not exist (%s).', $request->getParameter('fac_id')));
    $Factura->delete();

    $this->redirect('factura/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $Factura = $form->save();

      $this->redirect('showFactura/fac_id?='.$Factura->getFacId());
    }
  }
}
